﻿using UnityEngine;
using System.Collections;
using System.IO;

public class Downloader : MonoBehaviour
{
    private WWW                     downloaderWWW;
        
    // 下载文件URL
    private string                  downloaderUrl;
    private const string            TEMP_ZIP_NAME = "test.zip";
        
    // 下载后存储的文件名
    private string                  downloaderFileName;
    public string                   DownloaderFileName
    {
        get {
            return downloaderFileName;
        }
    }

    // Use this for initialization
    void Start()
    {
        
    }

    void Update()
    {
        if (downloaderWWW != null && !downloaderWWW.isDone)
        {
            if (EventDownloadProgress != null)
            {
                EventDownloadProgress(downloaderWWW.progress);
            }
        }
    }        

    public static string Url2FileName(string url)
    {
        return Application.persistentDataPath + "//" + Path.GetFileName(url);
    }

    // for zip file
    public void Download(string url)
    {
        downloaderUrl = url;
        Debug.Log(" download url : " + downloaderUrl);

        //if (downloaderUrl.Contains(".zip"))
        //{
        //    downloaderFileName = Application.persistentDataPath + "//" + TEMP_ZIP_NAME;
        //}
        //else
        {
            downloaderFileName = Downloader.Url2FileName(downloaderUrl);
        }

        StartCoroutine(DownloadAction());
    }

    private IEnumerator DownloadAction()
    {
        using (downloaderWWW = new WWW(downloaderUrl))
        {
            yield return downloaderWWW;

            if (!string.IsNullOrEmpty(downloaderWWW.error))
            {                   
                Debug.Log(downloaderWWW.error);

                StartCoroutine(DelayToInvoke.InvokeNextFrame(() =>
                {
                    if (EventDownloadFail != null)
                    {
                        EventDownloadFail(downloaderUrl);
                    }
                }));  
            }
            else
            {
                Debug.Log("write file : " + downloaderFileName);
                FileUtil.WriteFileByte(downloaderFileName, downloaderWWW.bytes, downloaderWWW.bytes.Length);

                StartCoroutine(DelayToInvoke.InvokeNextFrame(() =>
                {
                    if (EventDownloadSuccess != null)
                    {
                        EventDownloadSuccess(downloaderUrl);
                    }
                }));                

            }
        }
        downloaderWWW = null;    
    }

    #region Outlet Methods

    public delegate void OnDownloadSucces(string url);
    public delegate void OnDownloadFail(string url);
    public delegate void OnDownloadProgress(float value);
    public delegate void OnUncompressSuccess();
    public delegate void OnUncompressProgress(float value);
    public delegate void OnUncompressFail();

    public event OnDownloadSucces EventDownloadSuccess;
    public event OnDownloadFail EventDownloadFail;
    public event OnDownloadProgress EventDownloadProgress;
    //public event OnUncompressSuccess EventUncompressSuccess;
    //public event OnUncompressFail EventUncompressFail;
    //public event OnUncompressProgress EventUncompressProgress;
    #endregion // Outlet Methods

}

